package dao;

import bean.SignalEntity;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;

public class SignalEntityDaoImp implements SignalEntityDao {
    @Override
    public boolean addSingnalEntity(SignalEntity signalEntity) {
        try{
            Connection connection = null;
            PreparedStatement ps = null;
            connection = JdbcUtils.getConnection();
            String sql = "insert into mysignal (Data,TrainNumber,VehicleNumber,Bogie,Axle," +
                    "PathOfVibrate,PathOfTem) values(?,?,?,?,?,?,?)";
            ps = connection.prepareStatement(sql);
            ps.setString(1,signalEntity.getData());
            ps.setString(2,signalEntity.getTrainNumber());
            ps.setInt(3,signalEntity.getVehicleNumber() );
            ps.setInt(4,signalEntity.getBogie());
            ps.setInt(5,signalEntity.getAxle());
            ps.setString(6,signalEntity.getPathOfVibrate());
            ps.setString(7,signalEntity.getPathOfVibrate());
            System.out.println(ps.executeUpdate()+"条更新");

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteSingnalEntity(SignalEntity signalEntity) {
        return false;
    }

    @Override
    public List<SignalEntity> QuerySignalEntity(SignalEntity signalEntity) {
        List<SignalEntity> signalEntities = new LinkedList<>();
        try{
            Connection connection = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            connection = JdbcUtils.getConnection();
            String sql = "select * from mysignal where 1=1 ";
            if(!signalEntity.getData().equals("null"))
            {
                sql += " and Data=\""+signalEntity.getData()+"\"";
            }
            if(!signalEntity.getTrainNumber().equals("null"))
            {
                sql += " and TrainNumber=\""+signalEntity.getTrainNumber()+"\"";
            }
            if(signalEntity.getVehicleNumber()!=-1)
            {
                sql += " and VehicleNumber="+signalEntity.getVehicleNumber();
            }
            if((signalEntity.getBogie()!=-1))
            {
                sql += " and Bogie="+signalEntity.getBogie();
            }
            if(signalEntity.getAxle()!=-1)
            {
                sql += " and Axle="+signalEntity.getAxle();
            }
            System.out.println("sql:" + sql);
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();

            while(rs.next())
            {
                SignalEntity signalEntity1 = new SignalEntity();
                signalEntity1.setAxle(rs.getInt("Axle"));
                signalEntity1.setBogie(rs.getInt("Bogie"));
                signalEntity1.setData(rs.getString("Data"));
                signalEntity1.setPathOfTem(rs.getString("PathOfTem"));
                signalEntity1.setPathOfVibrate(rs.getString("PathOfVibrate"));
                signalEntity1.setTrainNumber(rs.getString("TrainNumber"));
                signalEntity1.setVehicleNumber(rs.getInt("VehicleNumber"));
                System.out.println(signalEntity1);
                signalEntities.add(signalEntity1);

            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return signalEntities;

    }
}
